package com.ymatou.iapi.dguser.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UpdateUnionIdBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.UpdateUnionIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*完善unionId信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateUnionId{
	private static UpdateUnionIdBean updateunionidBean;
	private static UpdateUnionIdCall updateunionidCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("完善unionId信息");
	}

	@Before
	public void caseUp() {
		updateunionidBean=new UpdateUnionIdBean();
		updateunionidCall=new UpdateUnionIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateunionidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateUnionId_001() {
		Logger.start(true,"完善unionId信息成功");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int user = DGUserCallService.UserCreateCall(u);
			updateunionidBean.setOpenId(u.getOpenId());
			updateunionidBean.setWxAppName("wxsqBuyer");
			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			Logger.verifyEquals("true", updateunionidCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(updateunionidBean.getOpenId());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			Logger.verifyEquals(user, list.get(0).getUserId(), "验证UserId");
			Logger.verifyEquals(updateunionidBean.getUnionId(), list.get(0).getUnionId(), "验证UnionId");
			Logger.verifyEquals(updateunionidBean.getWxAppName(), list.get(0).getWxAppName(), "验证getWxAppName");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UpdateUnionId_002() {
		Logger.start(true,"不输入openid失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int user = DGUserCallService.UserCreateCall(u);
//			updateunionidBean.setOpenId(u.getOpenId());
			updateunionidBean.setWxAppName("wxsqBuyer");
			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			Logger.verifyEquals("false", updateunionidCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UpdateUnionId_003() {
		Logger.start(true,"不输入unionId失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int user = DGUserCallService.UserCreateCall(u);
			updateunionidBean.setOpenId(u.getOpenId());
			updateunionidBean.setWxAppName("wxsqBuyer");
//			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			Logger.verifyEquals("false", updateunionidCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UpdateUnionId_004() {
		Logger.start(true,"不输入WxAppName失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int user = DGUserCallService.UserCreateCall(u);
			updateunionidBean.setOpenId(u.getOpenId());
//			updateunionidBean.setWxAppName("wxsqBuyer");
			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			Logger.verifyEquals("false", updateunionidCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//这里WxAppName不校验 查不到记录不更新
	public void Tc_UpdateUnionId_005() {
		Logger.start(true,"输入错误WxAppName失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int user = DGUserCallService.UserCreateCall(u);
			updateunionidBean.setOpenId(u.getOpenId());
			updateunionidBean.setWxAppName("1");
			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			Logger.verifyEquals("false", updateunionidCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UpdateUnionId_006() {
		Logger.start(true,"重复调用场景验证");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int user = DGUserCallService.UserCreateCall(u);
			updateunionidBean.setOpenId(u.getOpenId());
			updateunionidBean.setWxAppName("wxsqBuyer");
			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			updateunionidCall.callService();		
			Logger.verifyEquals("true", updateunionidCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(updateunionidBean.getOpenId());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			Logger.verifyEquals(user, list.get(0).getUserId(), "验证UserId");
			Logger.verifyEquals(updateunionidBean.getUnionId(), list.get(0).getUnionId(), "验证UnionId");
			Logger.verifyEquals(updateunionidBean.getWxAppName(), list.get(0).getWxAppName(), "验证getWxAppName");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UpdateUnionId_007() {
		Logger.start(true,"已有uid的情况下收到更改请求-不会更改uid");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			updateunionidBean.setOpenId(u.getOpenId());
			updateunionidBean.setWxAppName("wxsqBuyer");
			updateunionidBean.setUnionId("uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000);
			updateunionidCall.setData(updateunionidBean);
			updateunionidCall.callService();		
			Logger.verifyEquals("true", updateunionidCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(updateunionidBean.getOpenId());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			Logger.verifyEquals(user, list.get(0).getUserId(), "验证UserId");
			Logger.verifyEquals(u.getUnionId(), list.get(0).getUnionId(), "验证UnionId");
			Logger.verifyEquals(updateunionidBean.getWxAppName(), list.get(0).getWxAppName(), "验证getWxAppName");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}